#这个脚本用于计算各种客户数#
import requests
import time
import pymysql
ID = "ww7b4086d88a144eec"
SECRET = "Yy-hdts7KZHFDO8tzwWTNUfPiGyssUOo8XAPz2IOnbA"
url_get_access_token = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + ID + "&corpsecret=" + SECRET
print(url_get_access_token)

#获取access_token#
def get_access_token(ID,SECRET):
    access_token_json = requests.get(url_get_access_token).json()
    while access_token_json['errcode'] != 0:
        get_access_token(ID, SECRET)
        time.sleep(2)
    access_token = access_token_json['access_token']
    return access_token

db = pymysql.connect(host="localhost", user="root", password="zcxvbmn", database="weixindb")
cursor = db.cursor()

url_get_member_list = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=" + get_access_token(ID, SECRET) + "&department_id=1&fetch_child=1"

def clean_client_detail_list():
    clean_client_count_sql = "delete from client_detail_list"
    cursor.execute(clean_client_count_sql)
    db.commit()
    return

url_get_client_detail_list = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token=" + get_access_token(ID, SECRET)

def get_member_list():
    member_list = requests.get(url_get_member_list).json()['userlist']
    return member_list

def get_client_detail_list(userid):
    i = 1  # i为计数器
    client_list = []
    next_cursor = ''
    while i == 1 or next_cursor != '':
        user = {
            "userid": userid,
            "limit": 100,
            "cursor": next_cursor,
        }
        external_contact_list = requests.post(url_get_client_detail_list, json=user).json()['external_contact_list']
        for external_contact in external_contact_list:
            client_detail = {}
            client_detail['userid'] = repr(external_contact['follow_info']['userid'])
            client_detail['createtime'] = repr(external_contact['follow_info']['createtime'])
            client_detail['add_way'] = repr(external_contact['follow_info']['add_way'])
            client_detail['external_userid'] = repr(external_contact['external_contact']['external_userid'])
            client_detail['name'] = repr(external_contact['external_contact']['name'])
            client_detail['type'] = repr(external_contact['external_contact']['type'])
            client_detail['gender'] = repr(external_contact['external_contact']['gender'])
            cols = ", ".join('{}'.format(k) for k in client_detail.keys())
            val_cols = ', '.join('{}'.format(k) for k in client_detail.values())
            insert_sql ="insert into client_detail_list(%s) values(%s)" % (cols, val_cols)
            print(insert_sql)
            cursor.execute(insert_sql)
            db.commit()
        next_cursor = requests.post(url_get_client_detail_list, json=user).json()['next_cursor']
        i = i + 1
        #这里要开始尝试用executemany一次插入100条sql#
    return

#clean_client_detail_list()
start_time = time.time()
get_client_detail_list(userid="060310")
end_time = time.time()
print("插入耗时" + repr(end_time-start_time))